<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                xmlns:ds="http://deltaset.org/deltaset-faces"
                xmlns:mf="http://java.sun.com/jsf/composite/deltaset.meta/flow"
                xmlns:p="http://primefaces.org/ui"
                xmlns:composite="http://java.sun.com/jsf/composite"> 


    <p:dataTable var="tableItem" 
                 value="#{metaBean.lazyDataModel}" 
                 selectionMode="single" selection="#{metaBean.selectedItem}"                
                 paginator="true" rows="10"               
                 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                 rowsPerPageTemplate="5,10,15" >

        <p:ajax event="rowSelect" listener="#{metaBean.onRowSelect}" update="itemActions" />
        <p:column style="width:16px">  
            <p:rowToggler />  
        </p:column> 
        <c:forEach items="#{metaBean.listFields}" var="field">
            <p:column headerText="#{metaBean.labelField(field, metaBean.modelClass)}">
                <mf:output-list meta="#{metaBean}"
                                field="#{field}" 
                                value="#{tableItem[field.propertyName]}"/>
            </p:column>                       
        </c:forEach>               

        <p:column headerText="id">
            <h:outputText value="#{metaBean.getItemId(tableItem)}" />
        </p:column>
        
        <p:column headerText="Actions">

            <h:panelGroup>                
                <c:set var="recordId" value="#{metaBean.getItemId(tableItem)}"/>
                <c:set var="itemClassName" value="#{metaBean.getItemClassName(tableItem)}"/>
                <h:commandLink  value="View">
                    <f:ajax event="click" render="@form"/>
                    <f:param name="deltaset.meta.entityClassName" value="#{itemClassName}"/>
                    <f:param name="deltaset.meta.viewName" value="detailTemplate"/>
                    <f:param name="deltaset.meta.recordId" value="#{recordId}"/>
                </h:commandLink>
                <c:forEach items="#{metaBean.contextActions}" var="action">
                    <mf:commandButton metaAction="#{action}"
                                      model="#{tableItem}"
                                      meta="#{metaBean}"
                                      useItemParams="true"
                                      itemId="#{recordId}"
                                      itemClassName="#{itemClassName}"
                                      />                   
                </c:forEach>              
            </h:panelGroup>
       </p:column>


        <p:rowExpansion>                             

            <h:panelGroup>
                <div style="text-align: right">
                    <c:forEach items="#{metaBean.contextActions}" var="action">
                        <mf:commandButton metaAction="#{action}"
                                          model="#{tableItem}"
                                          meta="#{metaBean}"                                     
                                          />                   
                    </c:forEach>               

                    <div>
                        <c:forEach items="#{metaBean.otherContextActions}" var="action">                        
                            <mf:commandButton metaAction="#{action}"
                                              model="#{tableItem}"
                                              meta="#{metaBean}"
                                              noVoidShow="executeTemplate"
                                              voidShow="executeTemplate"                                          
                                              style="font-size: 0.8em;"
                                              />
                        </c:forEach>
                    </div>
                </div>
                <ds:fieldset>                    
                    <mf:detail-fields fields="#{metaBean.detailFields}" model="#{tableItem}" meta="#{metaBean}" owner="#{metaBean.modelClass}"/>
                </ds:fieldset>
                <div style="text-align: right">
                    <div>
                        <c:forEach items="#{metaBean.otherContextActions}" var="action">                        
                            <mf:commandButton metaAction="#{action}"
                                              model="#{tableItem}"
                                              meta="#{metaBean}"
                                              noVoidShow="executeTemplate"
                                              voidShow="executeTemplate"                                          
                                              style="font-size: 0.8em;"
                                              />
                        </c:forEach>
                    </div>
                    <c:forEach items="#{metaBean.contextActions}" var="action">
                        <mf:commandButton metaAction="#{action}"
                                          model="#{tableItem}"
                                          meta="#{metaBean}"                                     
                                          />                   
                    </c:forEach>               


                </div>
            </h:panelGroup> 

        </p:rowExpansion>

        <f:facet name="header">

            <h:panelGroup id="itemActions">
                <c:set var="selectedItem" value="#{metaBean.selectedItem}"/>
                <c:set var="recordId" value="#{metaBean.getItemId(selectedItem)}"/>
                <c:set var="itemClassName" value="#{metaBean.getItemClassName(selectedItem)}"/>
                <h:commandLink  value="View">
                    <f:ajax event="click" render="@form"/>
                    <f:param name="deltaset.meta.entityClassName" value="#{itemClassName}"/>
                    <f:param name="deltaset.meta.viewName" value="detailTemplate"/>
                    <f:param name="deltaset.meta.recordId" value="#{recordId}"/>
                </h:commandLink>
                <c:forEach items="#{metaBean.contextActions}" var="action">
                    <mf:commandButton metaAction="#{action}"
                                      model="#{selectedItem}"
                                      meta="#{metaBean}"
                                      useItemParams="true"
                                      itemId="#{recordId}"
                                      itemClassName="#{itemClassName}"
                                      />                   
                </c:forEach>              
            </h:panelGroup>
        </f:facet>

    </p:dataTable>  

</ui:composition>
